package com.kaishengit.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;


import com.kaishengit.pojo.Employee;
import com.kaishengit.pojo.Project;
import com.kaishengit.pojo.Project_Employee;
import com.kaishengit.service.EmployeeService;
import com.kaishengit.service.ProjectService;
import com.kaishengit.service.Project_EmployeeService;
import com.kaishengit.util.StringUtil;

@Results(value={
		@Result(name="list",location="main.jsp",type="dispatcher")
		})
public class ProjectAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private ProjectService projectService;
	private Project_EmployeeService project_EmployeeService;
	private EmployeeService employeeService;
	private Project project;
	private Project_Employee project_Employee;
	private Employee employee;

	@Action(value="toadd_project",results={
			@Result(name="success",location="add_project.jsp",type="dispatcher")
			})
	public String toadd_project(){
		return SUCCESS;
	}
	
	@Action(value="add_project",results={
			@Result(name="success",location="add_project.jsp",type="dispatcher")
			})
	public String add_project(){
		project.setId(StringUtil.getKey());
		project.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		
		
		project_Employee.setId(StringUtil.getKey());
		employee = employeeService.findById(project_Employee.getRole());
		project_Employee.setEmployee(employee);
		
		List<Project_Employee> list = new ArrayList<Project_Employee>();
		list.add(project_Employee);
		
		
		project.setProject_employee(list);
		project_Employee.setProject(project);
		
		
		projectService.save(project);
		project_EmployeeService.save(project_Employee);		
		
		return "list";
	}

	
	//get set
	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	

	

	public Project_Employee getProject_Employee() {
		return project_Employee;
	}

	public void setProject_Employee(Project_Employee project_Employee) {
		this.project_Employee = project_Employee;
	}
	
	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	//set
	@Autowired
	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}
	
	@Autowired
	public void setProject_EmployeeService(
			Project_EmployeeService project_EmployeeService) {
		this.project_EmployeeService = project_EmployeeService;
	}

	@Autowired
	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}
	
}
